package com.ruoyi.web.controller.points;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.points.domain.CommunityUserPoints;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.points.domain.CommunityPointsLog;
import com.ruoyi.points.service.ICommunityPointsLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分流水记录Controller
 * 
 * @author xianyu
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/points/log")
public class CommunityPointsLogController extends BaseController
{
    @Autowired
    private ICommunityPointsLogService communityPointsLogService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询积分流水记录列表
     */
    @PreAuthorize("@ss.hasPermi('points:log:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommunityPointsLog communityPointsLog)
    {
        startPage();
        Long userid=getUserId();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long roleIds=null;
        if (loginUser != null && loginUser.getUser() != null && loginUser.getUser().getRoles() != null && !loginUser.getUser().getRoles().isEmpty()) {
            Long roleId = loginUser.getUser().getRoles().get(0).getRoleId(); // 获取第一个角色的ID
            roleIds=roleId;
//            System.out.println(roleId);
        } else {
            System.out.println("该用户没有分配角色");
        }
        if (roleIds==1L){
            userid=null;
        }
        communityPointsLog.setUserId(userid);
        List<CommunityPointsLog> list = communityPointsLogService.selectCommunityPointsLogList(communityPointsLog);
        for (CommunityPointsLog item : list) {
            Long userId = item.getUserId(); // 获取 userId
            if (userId != null) {
                // 根据 userId 查询用户信息（假设 SysUser 是用户实体类）
                SysUser user = userService.selectUserById(userId);
                if (user != null) {
                    // 设置 userName
                    item.setUserName(user.getUserName());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出积分流水记录列表
     */
    @PreAuthorize("@ss.hasPermi('points:log:export')")
    @Log(title = "积分流水记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommunityPointsLog communityPointsLog)
    {
        List<CommunityPointsLog> list = communityPointsLogService.selectCommunityPointsLogList(communityPointsLog);
        ExcelUtil<CommunityPointsLog> util = new ExcelUtil<CommunityPointsLog>(CommunityPointsLog.class);
        util.exportExcel(response, list, "积分流水记录数据");
    }

    /**
     * 获取积分流水记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('points:log:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(communityPointsLogService.selectCommunityPointsLogById(id));
    }

    /**
     * 新增积分流水记录
     */
    @PreAuthorize("@ss.hasPermi('points:log:add')")
    @Log(title = "积分流水记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommunityPointsLog communityPointsLog)
    {
        return toAjax(communityPointsLogService.insertCommunityPointsLog(communityPointsLog));
    }

    /**
     * 修改积分流水记录
     */
    @PreAuthorize("@ss.hasPermi('points:log:edit')")
    @Log(title = "积分流水记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommunityPointsLog communityPointsLog)
    {
        return toAjax(communityPointsLogService.updateCommunityPointsLog(communityPointsLog));
    }

    /**
     * 删除积分流水记录
     */
    @PreAuthorize("@ss.hasPermi('points:log:remove')")
    @Log(title = "积分流水记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(communityPointsLogService.deleteCommunityPointsLogByIds(ids));
    }
}
